package com.billy.ohos.swipe.demo;

import com.billy.ohos.swipe.ConstantId;
import com.billy.ohos.swipe.SmartSwipe;
import com.billy.ohos.swipe.SmartSwipeWrapper;
import com.billy.ohos.swipe.SwipeConsumer;
import ohos.aafwk.ability.Ability;
import ohos.aafwk.content.Intent;
import ohos.agp.components.*;
import ohos.agp.window.service.Display;
import ohos.agp.window.service.DisplayManager;
import ohos.agp.window.service.WindowManager;

import java.util.List;


/**
 * base ability
 *
 * @author billy.qi
 */
public abstract class BaseAbility extends Ability {


    protected int screenWidth;
    protected int screenHeight;
    private Text mTvTitle;
    protected Image mIvLeftBtn;
    protected ComponentContainer contentView;


    @Override
    protected void onStart(Intent intent) {
        super.onStart(intent);
        //        setRequestedOrientation(AbilityInfo.SCREEN_ORIENTATION_PORTRAIT);
        Display dm = DisplayManager.getInstance().getDefaultDisplay(this).get();
        screenWidth = dm.getAttributes().width;
        screenHeight = dm.getAttributes().height;
        getWindow().addFlags(WindowManager.LayoutConfig.MARK_TRANSLUCENT_STATUS);   //沉浸式状态栏
    }


    @Override
    public void setUIContent(ComponentContainer componentContainer) {
        contentView = componentContainer;
        contentView.setId(ConstantId.ABLITY_CONTENT_ID);
        super.setUIContent(contentView);
        initView();

    }


    public void setContentView(int layoutResID) {
        contentView = (ComponentContainer) LayoutScatter.getInstance(this).parse(layoutResID, null, false);
        contentView.setId(ConstantId.ABLITY_CONTENT_ID);
        super.setUIContent(layoutResID);
        initView();
    }

    private void initView() {
        mIvLeftBtn = (Image) findComponentById(ResourceTable.Id_iv_left);
        mTvTitle = (Text) findComponentById(ResourceTable.Id_tv_title);
        if (mTvTitle == null) {
            return;
        }
        int resId = getTitleResId();
        if (resId != 0) {
            mTvTitle.setText(resId);
        } else {
            mTvTitle.setText(getTitleStr());
        }

        if (mIvLeftBtn != null) {
            mIvLeftBtn.setClickedListener(new Component.ClickedListener() {
                @Override
                public void onClick(Component component) {
                    onLeftTitleBtnClick();
                }
            });
        }

    }

    protected void onLeftTitleBtnClick() {
        terminateAbility();
    }

    public String getTitleStr() {
        return "";
    }

    /**
     * getTitleResId
     *
     * @return @StringRes
     */
    public int getTitleResId() {
        return 0;
    }

    /**
     * setLeftBtnImage
     *
     * @param resId @DrawableRes
     */
    public void setLeftBtnImage(int resId) {
        if (mIvLeftBtn != null) {
            mIvLeftBtn.setPixelMap(resId);
        }
    }

    @Override
    public void onBackPressed() {
        SmartSwipeWrapper wrapper = SmartSwipe.peekWrapperFor(contentView);
        if (wrapper != null) {
            List<SwipeConsumer> consumers = wrapper.getAllConsumers();
            if (!consumers.isEmpty()) {
                for (SwipeConsumer consumer : consumers) {
                    if (consumer != null) {
                        if (consumer.isLeftEnable()) {
                            consumer.smoothLeftOpen();
                            return;
                        } else if (consumer.isTopEnable()) {
                            consumer.smoothTopOpen();
                            return;
                        } else {
                            super.onBackPressed();
                        }
                    }
                }
            }
        }
        super.onBackPressed();
    }
}
